﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Amba.NoteList.Fields;
using Amba.NoteList.Models;
using Amba.NoteList.Settings;
using Amba.NoteList.ViewModel;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;

namespace Amba.NoteList.Drivers
{
    public class NoteListFieldDriver : FieldDriverBase<NoteListField>
    {
        protected override DriverResult Display(ContentPart part, NoteListField field, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Fields_NoteList", GetDifferentiator(field, part),
                () =>
                {
                    return shapeHelper.Fields_NoteList(
                        Name: field.Name, 
                        Field: field, 
                        Settings: field.PartFieldDefinition.Settings.GetModel<NoteListFieldSettings>());
                });
        }

        protected override DriverResult Editor(ContentPart part, NoteListField field, dynamic shapeHelper)
        {
            return ContentShape("Fields_NoteList_Edit", GetDifferentiator(field, part),
                () =>
                {
                    var viewModel = new NoteListEditorViewModel
                    {
                        Field = field,
                        Data = string.IsNullOrWhiteSpace(field.Data) ? "[]" : field.Data,
                        Settings = field.PartFieldDefinition.Settings.GetModel<NoteListFieldSettings>()
                    };

                    return shapeHelper.EditorTemplate(TemplateName: "Fields_NoteList_Edit", Model: viewModel, Prefix: GetPrefix(field, part));
                });
        }

        protected override DriverResult Editor(ContentPart part, NoteListField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new NoteListEditorViewModel();
            
            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                field.Data = viewModel.Data;               
            }
            return Editor(part, field, shapeHelper);
        }

        protected override void Importing(ContentPart part, NoteListField field, ImportContentContext context)
        {
            var imageElement = context.Data.Element(field.FieldDefinition.Name + "." + field.Name);
            if (imageElement == null)
            {
                return;
            }

            var dataElement = imageElement.Element("Data");
            if (dataElement != null)
            {
                field.Data = dataElement.Value;
            }
        }

        protected override void Exporting(ContentPart part, NoteListField field, ExportContentContext context)
        {
            var imageElement = context.Element(field.FieldDefinition.Name + "." + field.Name);
            imageElement.Add(new XElement("Data", new XCData(field.Data)));
        }

    }
}